ggmap

Jan-Philipp Kolb and
Matthias Sand

04/13/2015

Library ggmap

library(ggmap)
qmap("Mannheim")

MA_map <- qmap("Mannheim",zoom=14)

Other zoom level

qmap(location = 'Mannheim', zoom = 12)

Get closer

qmap(location = 'Mannheim', zoom = 13)

Get very close

qmap(location = 'Mannheim', zoom = 20)

ggmap - source

qmap(location = 'Mannheim', zoom = 14, source="osm")

ggmap - maptype

qmap(location = 'Mannheim', zoom = 14, maptype="satellite")

ggmap - maptype

qmap(location = 'Mannheim', zoom = 14, maptype="hybrid")

ggmap - maptype

qmap(location = 'Mannheim', zoom = 14,
 maptype="toner",source="stamen")

Other ways to get a map

MAmap <- get_googlemap("Mannheim")
ggmap(MAmap)

Geocoding

Geocoding (…) uses a description of a location, most typically a postal address or place name, to find geographic coordinates from spatial reference data …

Wikipedia - Geocoding

geocode("Mannheim University")
lon lat
8.462233 49.48371

Geocoding - points of interest

POI1 <- geocode("B2, 1 Mannheim")
POI2 <- geocode("Hbf Mannheim")
POI3 <- geocode("Wasserturm Mannheim")

POI1;POI2;POI3
##        lon      lat
## 1 8.462844 49.48569
##        lon      lat
## 1 8.469879 49.47972
##        lon      lat
## 1 8.473664 49.48483

Geocoding - with a loop

POI <- c("B2, 1 Mannheim","Hbf Mannheim",
         "Wasserturm Mannheim")

ListPOI <- data.frame(lat=NA,lon=NA)

for ( i in 1:length(POI)){
  geoPOI <- geocode(POI[i])
  ListPOI[i,"lat"] <-  geoPOI$lat 
  ListPOI[i,"lon"] <-  geoPOI$lon 
}

Points in map

Article by David Kahle and Hadley Wickham on the usage of ggmap.

MA_map +
geom_point(aes(x = lon, y = lat),
data = ListPOI)

More about adding points

pic

ggmap - adding colors

ListPOI$color <- c("A","B","C")
MA_map +
geom_point(aes(x = lon, y = lat,col=color),
data = ListPOI)

ggmap - bigger dots

ListPOI$size <- c(10,20,30)
MA_map +
geom_point(aes(x = lon, y = lat,col=color,size=size),
data = ListPOI)

Cheatsheet

pic

Get the distance between 2 points

mapdist("Q1, 4 Mannheim","B2, 1 Mannheim")
##             from             to   m    km     miles seconds  minutes
## 1 Q1, 4 Mannheim B2, 1 Mannheim 746 0.746 0.4635644     209 3.483333
##        hours
## 1 0.05805556
mapdist("Q1, 4 Mannheim","B2, 1 Mannheim",mode="walking")
##             from             to   m    km     miles seconds  minutes
## 1 Q1, 4 Mannheim B2, 1 Mannheim 546 0.546 0.3392844     421 7.016667
##       hours
## 1 0.1169444

Get another distance

mapdist("Q1, 4 Mannheim","B2, 1 Mannheim",mode="bicycling")
##             from             to   m    km    miles seconds  minutes
## 1 Q1, 4 Mannheim B2, 1 Mannheim 555 0.555 0.344877     215 3.583333
##        hours
## 1 0.05972222